package com.demo.controller;

import com.demo.pojo.Admin;
import com.demo.pojo.User;
import com.demo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class AdminController {
    @Autowired
    @Qualifier("adminServiceImpl")
    private AdminService adminService;

    @RequestMapping("/toqueryuser")
    public String toqueryuser(){
        return "query_user";
    }

    @RequestMapping("/queryuser")
    public String queryuser(String username, Model model){
        User user = adminService.adminSelectUserByName(username);
        if (user!=null){
            model.addAttribute("selectuser", user);
        }
        else{
            model.addAttribute("querymsg","此用户不存在。");
        }
        return "query_user";
    }

    @RequestMapping("/toupdateuser")
    public String toupdateuser(){
        return "updateuser";
    }

    @RequestMapping("/updateuser")
    public String updateuser(String username, String userpassword, String tel, Model model){
        User newuser = adminService.adminSelectUserByName(username);
        if(userpassword!=null && !userpassword.equals("")){
            newuser.setUserpassword(userpassword);
        }
        if(tel!=null && !tel.equals("")){
            newuser.setTel(tel);
        }
        int i = adminService.adminUpdateUser(newuser);
        if(i>=1){
            model.addAttribute("updateusermsg", "修改成功");
        }
        else {
            model.addAttribute("updateusermsg", "修改失败");
        }

        return "updateuser";
    }

    @RequestMapping("/toadduser")
    public String toadduser(){
        return "adduser";
    }

    @RequestMapping("/adduser")
    public String adduser(User user, Model model){
        if (adminService.adminSelectUserByName(user.getUsername())!=null){
            model.addAttribute("addusermsg", "此用户名已被注册");
            return "adduser";
        }
        int i = adminService.adminAddUser(user);
        if(i==0){
            model.addAttribute("addusermsg", "添加失败");
            return "adduser";
        }
        else {
            model.addAttribute("addusermsg", "添加成功");
            return "adduser";
        }
    }

    @RequestMapping("/todeleteuser")
    public String todeleteuser(){
        return "admin_deleteuser";
    }

    @RequestMapping("/deleteuser")
    public String deleteuser(String username,Model model){
        User user = adminService.adminSelectUserByName(username);
        if (user.getBalance()>0){
            model.addAttribute("deleteusermsg", "该账户余额不为0，无法删除。");
            return "admin_deleteuser";
        }
        int i = adminService.adminDeleteUserByName(username);
        if(i==0){
            model.addAttribute("deleteusermsg", "删除失败");
            return "admin_deleteuser";
        }
        else {
            model.addAttribute("deleteusermsg", "删除成功");
            return "admin_deleteuser";
        }
    }

    @RequestMapping("/toupdateadmin")
    public String toupdateadmin(){
        return "updateadmin";
    }

    @RequestMapping("/updateadmin")
    public String updateadmin(String adminpassword, Model model, HttpServletRequest httpServletRequest){
        Admin admin = (Admin) httpServletRequest.getSession().getAttribute("admin");
        admin.setAdminpassword(adminpassword);
        int i = adminService.updateAdmin(admin);
        if(i==0){
            model.addAttribute("updateadminmsg", "修改失败");
            return "updateadmin";
        }
        else {
            model.addAttribute("updateadminmsg", "修改成功");
            return "updateadmin";
        }
    }
}
